package com.zero2oneit.mall.goods.controller;

import com.zero2oneit.mall.common.bean.goods.CommunityCategory;
import com.zero2oneit.mall.common.bean.goods.GoodsCategory;
import com.zero2oneit.mall.common.bean.goods.GoodsProductInfo;
import com.zero2oneit.mall.common.bean.goods.GoodsProductInfoRelation;
import com.zero2oneit.mall.common.utils.R;
import com.zero2oneit.mall.common.utils.snowflake.IdWorkerFactory;
import com.zero2oneit.mall.goods.service.GoodsProductInfoRelationService;
import com.zero2oneit.mall.goods.service.GoodsProductInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * Description: 商品管理（社区）
 *
 * @author Tg
 * @email zero2oneit@163.com
 * @date 2021-02-22
 */
@RestController
@RequestMapping("/admin/communityGoods")
public class GoodsProductInfoController {

    @Autowired
    private GoodsProductInfoService goodsProductService;

    @Autowired
    private GoodsProductInfoRelationService goodsRelationService;


    /**
     * 添加或编辑商品分类（社区团购）信息
     * @param goods
     * @return
     */
    @PostMapping("/addOrEdit")
    public R addOrEdit(@RequestBody GoodsProductInfo goods){

        goodsProductService.saveOrUpdate(goods);

        for (int i = 0; i < goods.getMarketPrice().split(",").length; i++) {
            GoodsProductInfoRelation relation = new GoodsProductInfoRelation();
            relation.setId(IdWorkerFactory.create(0,0).nextId());
            relation.setProductId(goods.getProductId());
            relation.setProductName(goods.getProductName());
            relation.setMarketPrice(new BigDecimal(goods.getMarketPrice().split(",")[i]));
            relation.setSupplyPrice(new BigDecimal(goods.getSupplyPrice().split(",")[i]));
            relation.setProductPrice(new BigDecimal(goods.getProductPrice().split(",")[i]));
            relation.setProductStock(Integer.valueOf(goods.getProductStock().split(",")[i]));
            relation.setSaleAreaName(goods.getSaleAreaName().split(",")[i]);
            goodsRelationService.saveOrUpdate(relation);
        }

        return R.ok();
    }

}
